﻿using MatlabWork.Dal;
using MatlabWork.Entity;
using MatlabWork.Model;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using TestingPlatForm.MissionContrl.Dal;
using TestingPlatForm.MissionContrl.Entity;

namespace TestingPlatForm.MissionContrl.ViewModels
{
    public class CalculatorViewModel : BindableBase
    {
        #region
        private double _longitude;

        public double longitude
        {
            get { return _longitude; }
            set { SetProperty(ref _longitude, value); }
        }

        private double _latitude;

        public double latitude
        {
            get { return _latitude; }
            set { SetProperty(ref _latitude, value); }
        }

        private double _altitude;

        public double altitude
        {
            get { return _altitude; }
            set { SetProperty(ref _altitude, value); }
        }

        private double x;

        public double X
        {
            get { return x; }
            set { SetProperty(ref x, value); }
        }

        private double y;

        public double Y
        {
            get { return y; }
            set { SetProperty(ref y, value); }
        }

        private double z;

        public double Z
        {
            get { return z; }
            set { SetProperty(ref z, value); }
        }

        private string _Grid_BS;

        public string Grid_BS
        {
            get { return _Grid_BS; }
            set { SetProperty(ref _Grid_BS, value); }
        }


        private string _ChangeTitude;

        public string ChangeTitude
        {
            get { return _ChangeTitude; }
            set { SetProperty(ref _ChangeTitude, value); }
        }

        private string _UTCTime;

        public string UTCTime
        {
            get { return _UTCTime; }
            set { SetProperty(ref _UTCTime, value); }
        }

        private int _TimeSpan;

        public int TimeSpan
        {
            get { return _TimeSpan; }
            set { SetProperty(ref _TimeSpan, value); }
        }

        private int _TimeSpan_2020;

        public int TimeSpan_2020
        {
            get { return _TimeSpan_2020; }
            set { SetProperty(ref _TimeSpan_2020, value); }
        }

        private int _TimeSpan_DBT;

        public int TimeSpan_DBT
        {
            get { return _TimeSpan_DBT; }
            set { SetProperty(ref _TimeSpan_DBT, value); }
        }

        #endregion

        public Calculate calculate = new Calculate();

        public CalculatorViewModel()
        {

        }

        public ICommand ToECEFCommand
        {
            get => new DelegateCommand(() =>
            {

                Titude titude = new Titude();
                try
                {
                    titude.longitude = longitude;
                    titude.latitude = latitude;
                    titude.altitude = altitude;
                    titude = calculate.ConvertGeodeticToECEF(titude);
                    X = titude.X;
                    Y = titude.Y;
                    Z = titude.Z;
                }
                catch (Exception)
                {

                    throw;
                }
            });
        }

        public ICommand FROMECEFCommand
        {
            get => new DelegateCommand(() =>
            {

                Titude titude = new Titude();
                try
                {
                    titude.X = X;
                    titude.Y = Y;
                    titude.Z = Z;
                    titude = calculate.ECEFToLatLon(titude);
                    longitude = titude.longitude;
                    latitude = titude.latitude;
                    altitude = titude.altitude;
                }
                catch (Exception)
                {

                    throw;
                }
            });
        }

        public ICommand longitudeCommand
        {
            get => new DelegateCommand(() =>
            {
                ChangeTitude = calculate.DecimalToDMS(longitude);
            });
        }

        public ICommand latitudeCommand
        {
            get => new DelegateCommand(() =>
            {
                ChangeTitude = calculate.DecimalToDMS(latitude);
            });
        }

        public ICommand UTCCommand
        {
            get => new DelegateCommand(() =>
            {
                DateTime time = new DateTime(1970, 1, 1);
                DateTime newDate = time.AddSeconds(TimeSpan);
                UTCTime = newDate.ToString();
            });
        }

        public ICommand SpanCommand
        {
            get => new DelegateCommand(() =>
            {
                if (!String.IsNullOrEmpty(UTCTime))
                {
                    TimeSpan = (int)calculate.UTCToTimeSpan(DateTime.Parse(UTCTime));
                }
            });
        }

        public ICommand UTC2Command
        {
            get => new DelegateCommand(() =>
            {
                DateTime UTC = calculate.TimeSpanToUTC(TimeSpan_2020);
                UTCTime = UTC.ToString();

            });
        }

        public ICommand Span2Command
        {
            get => new DelegateCommand(() =>
            {
                if (!String.IsNullOrEmpty(UTCTime))
                {

                    DateTime time = new DateTime(2020, 1, 1);
                    TimeSpan timeSpan = DateTime.Parse(UTCTime) - time;
                    TimeSpan_2020 = (int)timeSpan.TotalSeconds;
                }
            });
        }

        public ICommand BDTCommand
        {
            get => new DelegateCommand(() =>
            {
                DateTime time = new DateTime(2006, 1, 1);
                UTCTime = time.AddSeconds(TimeSpan_DBT).ToString();
            });
        }

        public ICommand BDTSpanCommand
        {
            get => new DelegateCommand(() =>
            {
                if (!String.IsNullOrEmpty(UTCTime))
                {
                    DateTime time = new DateTime(2006, 1, 1);
                    TimeSpan timeSpan = DateTime.Parse(UTCTime) - time;
                    TimeSpan_DBT = (int)timeSpan.TotalSeconds;

                }
            });
        }

        public ICommand GridCommand
        {
            get => new DelegateCommand(() => 
            {
                Titude titude = new Titude();
                titude = InitialVal.XToXVI(int.Parse(Grid_BS));
                X = titude.X;
                Y = titude.Y;
                Z = titude.Z;
            });
        }

        public ICommand CalCommand
        {
            get => new DelegateCommand(() =>
            {

                OpenFileDialog dialog = new OpenFileDialog
                {
                    Filter = "All Files|*.*",

                };

                if (dialog.ShowDialog() == true && !string.IsNullOrWhiteSpace(dialog.FileName))
                {
                    // 获取用户选择的文件路径
                    string foldName = dialog.FileName;
                    string FileName = System.IO.Path.GetFileName(foldName);//扩展名 “.aspx”
                    ControlExcel controlExcel = new ControlExcel();
                    Dictionary<string, Titude> keyValues = controlExcel.readExcel(foldName);
                    controlExcel.insertExcel(foldName, keyValues);
                }

            });
        }
    }
}
